package com.atguigu.pamentdemo.service.impl;

import com.atguigu.pamentdemo.entity.OrderInfo;
import com.atguigu.pamentdemo.entity.RefundInfo;
import com.atguigu.pamentdemo.enums.wxpay.WxRefundStatus;
import com.atguigu.pamentdemo.mapper.refundInfoMapper;
import com.atguigu.pamentdemo.service.orderInfoService;
import com.atguigu.pamentdemo.service.refundInfoService;
import com.atguigu.pamentdemo.util.OrderNoUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

@Service
public class refundInfoServiceImpl extends ServiceImpl<refundInfoMapper, RefundInfo> implements refundInfoService {
    @Resource
    private orderInfoService orderInfoService;

    /**
     * 根据订单号创建退款订单
     *
     * @param orderNo
     * @param reason
     * @return
     */
    @Override
    public RefundInfo createRefundByOrderNo(String orderNo, String reason) {
        //根据订单号获取订单信息
        OrderInfo orderInfo = orderInfoService.getOrderByOrderNo(orderNo);

        //根据订单号生成退款订单
        RefundInfo refundInfo = new RefundInfo();
        //订单编号
        refundInfo.setOrderNo(orderNo);
        //退款单编号
        refundInfo.setRefundNo(OrderNoUtils.getRefundNo());
        //原订单金额
        refundInfo.setTotalFee(orderInfo.getTotalFee());
        //退款金额
        refundInfo.setRefund(orderInfo.getTotalFee());
        //退款原因
        refundInfo.setReason(reason);

        baseMapper.insert(refundInfo);
        return refundInfo;

    }

    /**
     * 更新退款记录
     * @param content
     */
    @Override
    public void updateRefund(String content) {
        Gson gson = new Gson();
        HashMap<String,String> hashMap = gson.fromJson(content, HashMap.class);

        //根据退款单变化给修改退款单

        RefundInfo refundInfo = new RefundInfo();

        refundInfo.setRefundId(hashMap.get("refund_id"));

        if (hashMap.get("status") != null){
            refundInfo.setRefundStatus(hashMap.get("status"));
            //将全部响应结果存入数据库的content字段
            refundInfo.setContentReturn(content);
        }

        if (hashMap.get("refund_status")!=null){
            //退款状态
            refundInfo.setRefundStatus(hashMap.get("refund_status"));
            //将全部响应结果存入数据库的content字段
            refundInfo.setContentNotify(content);
        }

        baseMapper.update(refundInfo,new LambdaQueryWrapper<RefundInfo>()
                .eq(RefundInfo::getRefundNo,hashMap.get("out_refund_no")));
    }

    /**
     * 找出申请退款超过minutes分钟并且未成功的退款单
     * @param i
     * @return
     */
    @Override
    public List<RefundInfo> getNoRefundOrderByDuration(int i) {
        //minutes分钟之前的时间
        LocalDateTime localDateTime = LocalDateTime.now().minusMinutes(i);
        List<RefundInfo> refundInfoList = baseMapper.selectList(new LambdaQueryWrapper<RefundInfo>()
                .eq(RefundInfo::getRefundStatus, WxRefundStatus.PROCESSING.getType())
                .le(RefundInfo::getCreateTime, localDateTime));

        return refundInfoList;
    }
}
